This page last changed on Feb 28, 2007 by fruminator.

This is a page to post Snippets of useful SLD to make pretty maps.

See also: Google Maps SLD and Geoserver and Google SLDs for some full SLD files that echo the Google Maps style. The second in particular has a lot of zoom levels and complex stuff going on.

The following SLD code shows Points (GEOM) only in a given scale (from Nuno Faria):

<?xml version="1.0" encoding="UTF-8"?>
<StyledLayerDescriptor version="1.0.0"
 xsi:schemaLocation="http://www.opengis.net/sld StyledLayerDescriptor.xsd"
 xmlns="http://www.opengis.net/sld"
 xmlns:ogc="http://www.opengis.net/ogc"
 xmlns:xlink="http://www.w3.org/1999/xlink"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <NamedLayer>
    <Name>Default Styler</Name>
      <UserStyle>
        <FeatureTypeStyle>
          <FeatureTypeName>Feature</FeatureTypeName>
          <Rule>
            <MinScaleDenominator>0.1</MinScaleDenominator>
            <MaxScaleDenominator>10</MaxScaleDenominator>
            <PointSymbolizer>
              <Graphic>
                <ExternalGraphic>
                  <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="forest_fill2.png"/>
                  <Format>image/png</Format>
                </ExternalGraphic>
                <Mark/>
                <Size>35.0</Size>
              </Graphic>
            </PointSymbolizer>
          </Rule>
        </FeatureTypeStyle>
      </UserStyle>
    </NamedLayer>
</StyledLayerDescriptor>

The following SLD code shows a external graphic along a line (from Aleda Freeman):

<sld:StyledLayerDescriptor version="1.0.0" xmlns:gml="http://www.opengis.net/gml"
  xmlns:ows="http://www.opengis.net/ows" xmlns:sld="http://www.opengis.net/sld"
  xmlns:wms="http://www.opengis.net/ows" xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.opengis.net/ows http://sun420:8888/geoserver/schemas/sld/GetMap.xsd">
  <sld:NamedLayer>
   <UserStyle xmlns="http://www.opengis.net/sld">
    <FeatureTypeStyle>
     <Rule>
      <LineSymbolizer>
       <Stroke>
        <sld:GraphicStroke>
         <sld:Graphic>
          <sld:ExternalGraphic>
           <sld:OnlineResource
            xlink:href="http://maps.massgis.state.ma.us/images/question_mark.gif"/>
           <sld:Format>image/gif</sld:Format>
          </sld:ExternalGraphic>
          <sld:Size>19</sld:Size>
         </sld:Graphic>
        </sld:GraphicStroke>
       </Stroke>
      </LineSymbolizer>
     </Rule>
    </FeatureTypeStyle>
   </UserStyle>
  </sld:NamedLayer>
 </sld:StyledLayerDescriptor>

Screenshot of above:

Many more snippets available at: http://maps.massgis.state.ma.us/geoserver_sld/

The following SLD allows one to make a POST request to geoserver. The form actions needs to be defined as "http://localhost:8080/geoserver/wms/GetMap". Adjust hostname and port number to you settings. The example produces the same result as the getMap.url demo in Geoserver (from Alex Petkov:

<?xml version="1.0" encoding="UTF-8"?>
<ogc:GetMap
 xmlns:ogc="http://www.opengis.net/ows"
 xmlns:gml="http://www.opengis.net/gml"
 version="1.1.1" service="WMS">
  <StyledLayerDescriptor version="1.0.0">
   <NamedLayer>
    <Name>topp:states</Name>
     <NamedStyle>
      <Name>population</Name>
     </NamedStyle>
    </NamedLayer>
   </StyledLayerDescriptor>
   <BoundingBox srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">
    <gml:coord>
     <gml:X>-130.0</gml:X>
     <gml:Y>24.0</gml:Y>
    </gml:coord>
    <gml:coord>
     <gml:X>-66.0</gml:X>
     <gml:Y>50.0</gml:Y>
    </gml:coord>
   </BoundingBox>
   <Output><Format>image/png</Format>
    <Size>
     <Width>600</Width>
     <Height>320</Height>
    </Size>
   </Output>
   <Exceptions>application/vnd.ogc.se+xml</Exceptions>
  </ogc:GetMap>

Here is another example sld for filtering by bounding box and parameter value:

<?xml version="1.0" encoding="UTF-8"?>
<ogc:GetMap
 xmlns:ogc="http://www.opengis.net/ows"
 xmlns:gml="http://www.opengis.net/gml"
 version="1.1.1" service="WMS">
  <StyledLayerDescriptor version="1.0.0"> <NamedLayer>
  <Name>geo:USGS_Quat_Faults</Name>
  <UserStyle>
  <Name>geothermalgeo:USGS_Quat_Faults</Name>
  <Title>Geothermal</Title>
  <Abstract>Geothermal filters</Abstract>
    <FeatureTypeStyle>
      <Rule>
             <!-- Theme 1 -->
              <ogc:Filter xmlns:gml="http://www.opengis.net/gml">
                <ogc:PropertyIsBetween>
                  <ogc:PropertyName>SLIPRATE</ogc:PropertyName>
                  <ogc:LowerBoundary>
                    <ogc:Literal>0.001</ogc:Literal>
                  </ogc:LowerBoundary>
                  <ogc:UpperBoundary>
                    <ogc:Literal>17.5005</ogc:Literal>
                  </ogc:UpperBoundary>
                </ogc:PropertyIsBetween>
              </ogc:Filter>
        <LineSymbolizer>
          <Stroke>
            <CssParameter name="stroke">
              <ogc:Literal>#0000FF</ogc:Literal>
            </CssParameter>
            <CssParameter name="stroke-width">
              <ogc:Literal>2</ogc:Literal>
            </CssParameter>
          </Stroke>
        </LineSymbolizer>
      </Rule>
      <Rule>
             <!-- Theme 2 -->
              <ogc:Filter xmlns:gml="http://www.opengis.net/gml">
                <ogc:PropertyIsBetween>
                  <ogc:PropertyName>SLIPRATE</ogc:PropertyName>
                  <ogc:LowerBoundary>
                    <ogc:Literal>17.5005</ogc:Literal>
                  </ogc:LowerBoundary>
                  <ogc:UpperBoundary>
                    <ogc:Literal>35</ogc:Literal>
                  </ogc:UpperBoundary>
                </ogc:PropertyIsBetween>
              </ogc:Filter>
        <LineSymbolizer>
          <Stroke>
            <CssParameter name="stroke">
              <ogc:Literal>#FF0000</ogc:Literal>
            </CssParameter>
            <CssParameter name="stroke-width">
              <ogc:Literal>2</ogc:Literal>
            </CssParameter>
          </Stroke>
        </LineSymbolizer>
      </Rule>
  </FeatureTypeStyle>
  </UserStyle>
</NamedLayer>
</StyledLayerDescriptor>
   <BoundingBox srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">
    <gml:coord>
     <gml:X>-116.01796340942383</gml:X>
     <gml:Y>36.40119743347168</gml:Y>
    </gml:coord>
    <gml:coord>
     <gml:X>-114.8682632446289</gml:X>
     <gml:Y>37.33532905578613</gml:Y>
    </gml:coord>
   </BoundingBox>
   <Output><Format>image/jpeg</Format>
    <Size>
     <Width>1000</Width>
     <Height>813</Height>
    </Size>
   </Output>
   <Exceptions>application/vnd.ogc.se+xml</Exceptions>
  </ogc:GetMap>


The above sld file was created for use in a java Post servlet. The following shows the java snippet for a Post to a local Geoserver and the returned image sent to the Http response. The 'sld' StringBuffer holds the sld like the one shown above which is created elsewhere in the servlet code.

PrintWriter xmlOut = null;

  try {
   // create wms connection
   URL u = new URL("http://local:8080/geoserver/wms/GetMap");
   HttpURLConnection geocon = (HttpURLConnection)u.openConnection();
   geocon.setAllowUserInteraction(false);
   geocon.setRequestMethod("POST");
   geocon.setRequestProperty("Content-Type", "application/xml");
   geocon.setDoOutput(true);
   geocon.setDoInput(true);
   geocon.setUseCaches(false);
  //send sld as String to the wms connection
   System.out.println("GetImage\n"+sld.toString());
   xmlOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(geocon.getOutputStream())));
   xmlOut.write(sld.toString());
   xmlOut.flush();
   xmlOut.close();
  //get the wms connection response which in this case is a jpeg and forward as http response
  //note: cache is turned off since the image is dynamically created
   response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
   response.addHeader("Cache-Control", "post-check=0, pre-check=0");
   response.setHeader("Pragma", "no-cache");
   response.setContentType("image/jpeg");
   OutputStream outsld = response.getOutputStream();
   int c;
   InputStream in = geocon.getInputStream();
   while ((c = in.read()) \!= \-1) outsld.write(c);
   in.close();
   outsld.close();

  } catch (IOException e) {
   e.printStackTrace();
  } catch&nbsp; (Exception e) {
   System.out.println("WMSFeature: "+e.toString());
}

Database-driven Styling (here we assume PostGIS or other SQL-like system as data backend) -- what if you actually hate XML and SLD but want to style your maps in very data-driven ways.  For example, coloring the area of a given census tract by its average income, or sizing the dot for a city by its population.  One possible approach is to break the wall between data and presentation, storing the correct color and/or other parameters in columns in your database, and pass those values through into the SLD using the  <ogc:PropertyName> tag.

For example, assume that you have a table called census_tracts with a column called income_color that stores the color you'd like to use for each tract, that you have somehow generated based on the income for each tract. Here's how you would pass thru that color value into the styling for your map:

              ...
           <PolygonSymbolizer>
            <Fill>
              <CssParameter name="fill"><ogc:PropertyName>income_color</ogc:PropertyName></CssParameter>
              <CssParameter name="fill-opacity">0.5</CssParameter>
            </Fill>
            <Stroke>
              <CssParameter name="stroke"><ogc:PropertyName>income_color</ogc:PropertyName></CssParameter>
              <CssParameter name="stroke-width">1</CssParameter>
            </Stroke>
          </PolygonSymbolizer>
            ...

example.sld (text/plain)
question_marks_line.png (image/png)
Document generated by Confluence on Jan 16, 2008 23:27